﻿using System;

namespace Yaml.Representation.Nodes {
    public class Node {
        private string tag;
        private object value;
        private int hash = -1;

        public Node(string tag, object value) {
            this.tag = tag;
            this.value = value;
        }

        public string Tag {
            get { return this.tag; }
        }

        public object Value {
            get { return this.value; }
        }

        public override int GetHashCode() {
            if (hash == -1) {
                hash = 3;
                hash += (null == tag) ? 0 : 3 * tag.GetHashCode();
                hash += (null == value) ? 0 : 3 * value.GetHashCode();
            }
            return hash;
        }

        public override bool Equals(object obj) {
            if (obj is Node) {
                Node nod = (Node)obj;
                return ((this.tag != null) ? this.tag.Equals(nod.tag) : nod.tag == null) &&
                    ((this.value != null) ? this.value.Equals(nod.value) : nod.value == null);
            }
            return false;
        }

        public override string ToString() {
            string name = this.GetType().Name;
            return "#<" + name + " (tag=" + Tag + ", value=" + Value + ")>";
        }
    }
}
